package com.bountytask.controller;

import com.alibaba.fastjson.JSONObject;
import com.bountytask.entity.Collect;
import com.bountytask.service.CollectService;
import com.bountytask.service.impl.CollectServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 添加收藏任务
     */
    @PostMapping("/collect/add")
    public JSONObject addCollectTack(@RequestBody JSONObject recData) {
        return collectService.addTask(recData.getLong("userid"),recData.getLong("taskid"));
    }

    /**
    * 删除收藏任务
    * @param recData
    * @return
    */

    @PutMapping("/collect/delete")
    public JSONObject deleteCollectTask(@RequestBody JSONObject recData){
        int deleteTask=collectService.deleteTask(recData.getLong("userid"),recData.getLong("taskid"));
        JSONObject jsonObject=new JSONObject(true);
        if (deleteTask!=0)
        {
            jsonObject.put("code",200);
            jsonObject.put("message","成功");
        }else
        {
            jsonObject.put("code",400);
            jsonObject.put("message","失败");
        }
        return jsonObject;
    }

    /**
     * 查询用户全部收藏任务
     * @return
     */
    @GetMapping("/collect/_mget/{userid}")
    public JSONObject getCollectTask(@PathVariable long userid)
    {
        List <Collect> Collect = collectService.getCollectAll(userid);
        JSONObject jsonObject=new JSONObject(true);
        jsonObject.put("code",200);
        jsonObject.put("message","成功");
        jsonObject.put("data",Collect);
        return jsonObject;
    }
}
